package reverseStr;

public class Solution {
    public String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i+=2*k) {
            // 反转 [i,i+k-1]
            int start = i;
            int end = Math.min(i+k-1,chars.length-1);
            reverse(chars,start,end);
        }
        return new String(chars);
    }
    private void reverse(char[] s,int start,int end) {
        int i = start,j = end;
        while (i<j) {
            char t = s[i];
            s[i] = s[j];
            s[j] = t;
            i++;
            j--;
        }
    }
}
